﻿<!DOCTYPE html>
<html>
<head>
    <title>JS Objects Exercise 3</title>
    <link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <h1>Function that makes a deep copy of an object.</h1>
    <div id="js-console"></div>
    <script src="scripts/js-console.js"></script>
    <script>
        function deepCopy(obj)
        {
            var newObj = {};
            for (var prop in obj)
            {
                if (typeof obj[prop] == "object")
                {
                    newObj[prop] = deepCopy(obj[prop]);
                }
                else
                {
                    newObj[prop] = obj[prop];
                }
            }

            return newObj;
        }

        function createPoint(newX, newY) {
            return {
                X: newX,
                Y: newY,
                toString: function () {
                    return "(" + this.X + "," + this.Y + ")";
                }
            }
        }

        function createLine(newP1, newP2) {
            return {
                P1: newP1,
                P2: newP2,
                toString: function () {
                    return "(" + this.P1 + "," + this.P2 + ")";
                }
            }
        }

        var Point1 = createPoint(-2, -3);
        var Point2 = createPoint(-4, 4);

        var Line1 = createLine(Point1, Point2);
        var Line2 = createLine(Point1, Point2);
        var Line3 = deepCopy(Line1);

        jsConsole.writeLine("Original: L1:" + Line1 + " L2:" + Line2 + " L3:" + Line3);
        Line1.P1.X = 3.14;
        jsConsole.writeLine("Changed: L1:" + Line1 + " L2:" + Line2 + " L3:" + Line3);

    </script>
</body>
</html>
